package com.mvc.controller;
import com.alibaba.fastjson.JSON;
import com.mvc.entity.UserAddress;
import com.mvc.model.Result;
import com.mvc.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @ClassName UserAddressController
 * @Description TODO
 * @Author king
 * @Date 2021/11/27 16:26
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/user_address", produces = "text/html;charset=UTF-8")
public class UserAddressController {
    @Autowired
    private UserAddressService userAddressService;

    @RequestMapping(value = "/list.do")
    @ResponseBody
    public String listSearch(@RequestParam Integer page, @RequestParam Integer limit){
        Integer startRow = (page - 1) * limit;
        List<UserAddress> list = userAddressService.listSearch(startRow, limit);
        Integer count = userAddressService.getCount();
        Result result = new Result();
        result.setCode(0);
        result.setData(list);
        result.setCount(count);
        String json = JSON.toJSONString(result);
        return json;
    }
    @RequestMapping(value = "/add.do")
    @ResponseBody
    public String add(UserAddress UserAddress){
        Integer n = userAddressService.insert(UserAddress);
        Result result = new Result();
        if (n == 1){
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1);
            result.setMsg("失败");
        }
        String json = JSON.toJSONString(result);
        return json;
    }

    @RequestMapping(value = "/update.do")
    @ResponseBody
    public String update(UserAddress UserAddress){
        Integer n = userAddressService.update(UserAddress);
        Result result = new Result();
        if (n == 1){
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1);
            result.setMsg("失败");
        }
        String json = JSON.toJSONString(result);
        return json;
    }

    @RequestMapping(value = "/del.do")
    @ResponseBody
    public String del(@RequestParam Integer id){
        Integer n = userAddressService.del(id);
        Result result = new Result();
        if (n == 1){
            result.setCode(0);
            result.setMsg("成功");
        }else {
            result.setCode(1);
            result.setMsg("失败");
        }
        String json = JSON.toJSONString(result);
        return json;
    }
}